import Vue from 'vue'
import Vuex from 'vuex'
// 导入持久化插件
import persisted from 'vuex-persistedstate'
// 导入axios
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  //配置持久化存储数据的 vuex 插件
  plugins: [persisted()],
  state: {
    token: '',
    user: {},
  },
  getters: {
  },
  mutations: {
    setToken(state, token) {
      state.token = token
    },
    setUserInfo(state, user) {
      state.user = user
    }
  },
  actions: {
    async getUserInfo(context) {
      // 发送ajax请求个人信息
      const res = await axios.get('/my/userinfo')
      // 提交请求到mutations同步更新
      context.commit('setUserInfo', res.data.data)
    }
  },
  modules: {
  }
})
